/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package entityprojet;

import entityprojet.exceptions.IllegalOrphanException;
import entityprojet.exceptions.NonexistentEntityException;
import entityprojet.exceptions.PreexistingEntityException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityNotFoundException;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;

/**
 *
 * @author Ron
 */
public class FDepotemplJpaController implements Serializable {

    public FDepotemplJpaController(EntityManagerFactory emf) {
        this.emf = emf;
    }
    private EntityManagerFactory emf = null;

    public FDepotemplJpaController() {
        this.emf = Persistence.createEntityManagerFactory("projetT_RPU");
    }
    
    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(FDepotempl FDepotempl) throws IllegalOrphanException, PreexistingEntityException, Exception {
        if (FDepotempl.getFDepotCollection() == null) {
            FDepotempl.setFDepotCollection(new ArrayList<FDepot>());
        }
        List<String> illegalOrphanMessages = null;
        FDepot DENoOrphanCheck = FDepotempl.getDENo();
        if (DENoOrphanCheck != null) {
            FDepotempl oldCbDPNoDefautOfDENo = DENoOrphanCheck.getCbDPNoDefaut();
            if (oldCbDPNoDefautOfDENo != null) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("The FDepot " + DENoOrphanCheck + " already has an item of type FDepotempl whose DENo column cannot be null. Please make another selection for the DENo field.");
            }
        }
        if (illegalOrphanMessages != null) {
            throw new IllegalOrphanException(illegalOrphanMessages);
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            FDepot DENo = FDepotempl.getDENo();
            if (DENo != null) {
                DENo = em.getReference(DENo.getClass(), DENo.getCbMarq());
                FDepotempl.setDENo(DENo);
            }
            Collection<FDepot> attachedFDepotCollection = new ArrayList<FDepot>();
            for (FDepot FDepotCollectionFDepotToAttach : FDepotempl.getFDepotCollection()) {
                FDepotCollectionFDepotToAttach = em.getReference(FDepotCollectionFDepotToAttach.getClass(), FDepotCollectionFDepotToAttach.getCbMarq());
                attachedFDepotCollection.add(FDepotCollectionFDepotToAttach);
            }
            FDepotempl.setFDepotCollection(attachedFDepotCollection);
            em.persist(FDepotempl);
            if (DENo != null) {
                DENo.setCbDPNoDefaut(FDepotempl);
                DENo = em.merge(DENo);
            }
            for (FDepot FDepotCollectionFDepot : FDepotempl.getFDepotCollection()) {
                FDepotempl oldCbDPNoDefautOfFDepotCollectionFDepot = FDepotCollectionFDepot.getCbDPNoDefaut();
                FDepotCollectionFDepot.setCbDPNoDefaut(FDepotempl);
                FDepotCollectionFDepot = em.merge(FDepotCollectionFDepot);
                if (oldCbDPNoDefautOfFDepotCollectionFDepot != null) {
                    oldCbDPNoDefautOfFDepotCollectionFDepot.getFDepotCollection().remove(FDepotCollectionFDepot);
                    oldCbDPNoDefautOfFDepotCollectionFDepot = em.merge(oldCbDPNoDefautOfFDepotCollectionFDepot);
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            if (findFDepotempl(FDepotempl.getCbMarq()) != null) {
                throw new PreexistingEntityException("FDepotempl " + FDepotempl + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(FDepotempl FDepotempl) throws IllegalOrphanException, NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            FDepotempl persistentFDepotempl = em.find(FDepotempl.class, FDepotempl.getCbMarq());
            FDepot DENoOld = persistentFDepotempl.getDENo();
            FDepot DENoNew = FDepotempl.getDENo();
            Collection<FDepot> FDepotCollectionOld = persistentFDepotempl.getFDepotCollection();
            Collection<FDepot> FDepotCollectionNew = FDepotempl.getFDepotCollection();
            List<String> illegalOrphanMessages = null;
            if (DENoNew != null && !DENoNew.equals(DENoOld)) {
                FDepotempl oldCbDPNoDefautOfDENo = DENoNew.getCbDPNoDefaut();
                if (oldCbDPNoDefautOfDENo != null) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("The FDepot " + DENoNew + " already has an item of type FDepotempl whose DENo column cannot be null. Please make another selection for the DENo field.");
                }
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            if (DENoNew != null) {
                DENoNew = em.getReference(DENoNew.getClass(), DENoNew.getCbMarq());
                FDepotempl.setDENo(DENoNew);
            }
            Collection<FDepot> attachedFDepotCollectionNew = new ArrayList<FDepot>();
            for (FDepot FDepotCollectionNewFDepotToAttach : FDepotCollectionNew) {
                FDepotCollectionNewFDepotToAttach = em.getReference(FDepotCollectionNewFDepotToAttach.getClass(), FDepotCollectionNewFDepotToAttach.getCbMarq());
                attachedFDepotCollectionNew.add(FDepotCollectionNewFDepotToAttach);
            }
            FDepotCollectionNew = attachedFDepotCollectionNew;
            FDepotempl.setFDepotCollection(FDepotCollectionNew);
            FDepotempl = em.merge(FDepotempl);
            if (DENoOld != null && !DENoOld.equals(DENoNew)) {
                DENoOld.setCbDPNoDefaut(null);
                DENoOld = em.merge(DENoOld);
            }
            if (DENoNew != null && !DENoNew.equals(DENoOld)) {
                DENoNew.setCbDPNoDefaut(FDepotempl);
                DENoNew = em.merge(DENoNew);
            }
            for (FDepot FDepotCollectionOldFDepot : FDepotCollectionOld) {
                if (!FDepotCollectionNew.contains(FDepotCollectionOldFDepot)) {
                    FDepotCollectionOldFDepot.setCbDPNoDefaut(null);
                    FDepotCollectionOldFDepot = em.merge(FDepotCollectionOldFDepot);
                }
            }
            for (FDepot FDepotCollectionNewFDepot : FDepotCollectionNew) {
                if (!FDepotCollectionOld.contains(FDepotCollectionNewFDepot)) {
                    FDepotempl oldCbDPNoDefautOfFDepotCollectionNewFDepot = FDepotCollectionNewFDepot.getCbDPNoDefaut();
                    FDepotCollectionNewFDepot.setCbDPNoDefaut(FDepotempl);
                    FDepotCollectionNewFDepot = em.merge(FDepotCollectionNewFDepot);
                    if (oldCbDPNoDefautOfFDepotCollectionNewFDepot != null && !oldCbDPNoDefautOfFDepotCollectionNewFDepot.equals(FDepotempl)) {
                        oldCbDPNoDefautOfFDepotCollectionNewFDepot.getFDepotCollection().remove(FDepotCollectionNewFDepot);
                        oldCbDPNoDefautOfFDepotCollectionNewFDepot = em.merge(oldCbDPNoDefautOfFDepotCollectionNewFDepot);
                    }
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = FDepotempl.getCbMarq();
                if (findFDepotempl(id) == null) {
                    throw new NonexistentEntityException("The fDepotempl with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            FDepotempl FDepotempl;
            try {
                FDepotempl = em.getReference(FDepotempl.class, id);
                FDepotempl.getCbMarq();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The FDepotempl with id " + id + " no longer exists.", enfe);
            }
            FDepot DENo = FDepotempl.getDENo();
            if (DENo != null) {
                DENo.setCbDPNoDefaut(null);
                DENo = em.merge(DENo);
            }
            Collection<FDepot> FDepotCollection = FDepotempl.getFDepotCollection();
            for (FDepot FDepotCollectionFDepot : FDepotCollection) {
                FDepotCollectionFDepot.setCbDPNoDefaut(null);
                FDepotCollectionFDepot = em.merge(FDepotCollectionFDepot);
            }
            em.remove(FDepotempl);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<FDepotempl> findFDepotemplEntities() {
        return findFDepotemplEntities(true, -1, -1);
    }

    public List<FDepotempl> findFDepotemplEntities(int maxResults, int firstResult) {
        return findFDepotemplEntities(false, maxResults, firstResult);
    }

    private List<FDepotempl> findFDepotemplEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(FDepotempl.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public FDepotempl findFDepotempl(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(FDepotempl.class, id);
        } finally {
            em.close();
        }
    }

    public int getFDepotemplCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<FDepotempl> rt = cq.from(FDepotempl.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
